//UDP客户端
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define IP  "127.0.0.1"
#define SPORT 60000
#define SIZE  100

int main()
{
	//定义变量
	int sockfd = 0;
	socklen_t addrLength = 0;
	int data_int;
	char data;

	//创建socket  SOCK_DGRAM
	sockfd = socket( PF_INET, SOCK_DGRAM, 0);
	if ( 0 > sockfd )
	{
		perror("socket error");
		return -1;
	}	

	//设置对方的地址
	struct sockaddr_in addr = {
		.sin_family = PF_INET,
		.sin_port   = htons(SPORT),
		.sin_addr   = {
			.s_addr = inet_addr(IP),
		},
	};
	addrLength = sizeof(addr);
	//输入要发送的消息
	while (1) {
		printf("input data to bo sent\r\n");
		scanf("%x", &data_int);
		getchar();
		data = (char)(data_int & 0xff);
		//发送消息
		ssize_t ret = sendto(sockfd, &data, 1, 0,(struct sockaddr *)&addr, addrLength);
		if ( 0 > ret )
		{
			perror("client send msg error");
			close(sockfd);
			return -1;
		}
		printf("客户端发送消息成功，长度：%d\r\n", ret);
		//接收消息
	}

	close(sockfd);
	return 0;	
}
